package com.cqq.base.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cqq.auth.service.OrganizationService;
import com.cqq.auth.service.UserService;
import com.cqq.auth.service.bean.OrganizationBean;
import com.cqq.auth.service.bean.UserBean;
import com.cqq.common.core.util.BeanUtil;
import com.cqq.print.base.service.PPriceService;
import com.cqq.print.base.service.PProducePlanService;
import com.cqq.print.base.service.PPublisherService;
import com.cqq.print.base.service.bean.PPriceBean;
import com.cqq.print.base.service.bean.PProducePlanBean;
import com.cqq.print.base.service.bean.PPublisherBean;

/**
 * <h3>标签显示工具类实现</h3>
 * 
 * @author CHENQQ
 * @version 1.0.0
 * 
 */
@Component("tagUtil")
public class TagUtilImpl implements TagUtil {

	@Autowired
	private UserService userService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private PProducePlanService pProducePlanService;
	@Autowired
	private PPriceService pPriceService;
	@Autowired
	private PPublisherService pPublisherService;

	private Map<Integer, UserBean> userMap;
	
	private Map<Integer, OrganizationBean> orgMap;

	private Map<Integer,PProducePlanBean> proPlanMap;
	
	private Map<Integer,PPublisherBean> publisherMap;
	
	public TagUtilImpl() {
		this.userMap = new HashMap<Integer, UserBean>();
		this.orgMap = new HashMap<Integer, OrganizationBean>();
		this.proPlanMap = new HashMap<Integer, PProducePlanBean>();
		this.publisherMap = new HashMap<Integer, PPublisherBean>();
	}

	private UserBean getUser(Integer userId) {
		UserBean user = this.userMap.get(userId);
		if (user == null) {
			user = this.userService.findUser(userId);
			if (user != null) {
				this.userMap.put(user.getId(), user);
			}
		}
		return user;
	}
	
	private OrganizationBean getOrganization(Integer orgId) {
		OrganizationBean org = this.orgMap.get(orgId);
		if (org == null) {
			org = this.organizationService.findOrganization(orgId);
			if (org != null) {
				this.orgMap.put(org.getId(), org);
			}
		}
		return org;
	}

	public String getUsername(Integer userId) {
		if (userId == null) {
			return null;
		}
		UserBean user = this.getUser(userId);
		return user == null ? userId.toString() : user.getUsername(); 
	}
	
	
	public String getOrganizationName(Integer orgId) {
		if (orgId == null) {
			return null;
		}
		OrganizationBean org = this.getOrganization(orgId);
		return org == null ? orgId.toString() : org.getName();
	}

	
	public String getUsername(Integer[] userIds, String separator) {
		if (userIds == null || userIds.length == 0) {
			return null;
		}
		if (separator == null) {
			separator = ",";
		}
		StringBuffer sb = new StringBuffer();
		UserBean user = null;
		for (Integer id : userIds) {
			user = this.getUser(id);
			if (user == null) {
				sb.append(id).append(separator);
			} else {
				sb.append(user.getUsername()).append(separator);
			}
		}
		return sb.substring(0, sb.lastIndexOf(separator));
	}

	
	public String getRealName(Integer userId) {
		if (userId == null) {
			return null;
		}
		UserBean user = this.getUser(userId);
		return user == null ? userId.toString() : user.getRealName();
	}

	
	public String getRealName(Integer[] userIds, String separator) {
		if (userIds == null || userIds.length == 0) {
			return null;
		}
		if (separator == null) {
			separator = ",";
		}
		StringBuffer sb = new StringBuffer();
		UserBean user = null;
		for (Integer id : userIds) {
			user = this.getUser(id);
			if (user == null) {
				sb.append(id).append(separator);
			} else {
				sb.append(user.getRealName()).append(separator);
			}
		}
		return sb.substring(0, sb.lastIndexOf(separator));
	}

	public <S, T> String join(List<T> source, String fieldName, String separator) {
		return BeanUtil.joinByFieldName(source, fieldName, separator);
	}

	private PProducePlanBean getProPlan(Integer proPlanId){
		PProducePlanBean proPlan=this.proPlanMap.get(proPlanId);
		if(proPlan==null){
			proPlan=this.pProducePlanService.findProPlanById(proPlanId);
			if(proPlan!=null){
				this.proPlanMap.put(proPlan.getId(), proPlan);
			}
		}
		return proPlan;
	}
	
	public String getProPlanName(Integer proPlanId) {
		if(proPlanId==null){
			return null;
		}
		PProducePlanBean proPlan=this.getProPlan(proPlanId);
		return proPlan==null ? proPlanId.toString() : proPlan.getProPlanName();
	}

	public List<PProducePlanBean> getProducePlanList() {
		return this.pProducePlanService.queryAll();
	}
	
	public PPriceBean getPPriceByProPlanId(Integer proPlanId) {
		return pPriceService.getPPriceBeanByProPlanId(proPlanId);
	}

	private PPublisherBean getPPublisher(Integer pubId){
		PPublisherBean publisher=this.publisherMap.get(pubId);
		if(publisher==null){
			publisher=this.pPublisherService.queryById(pubId);
			if(publisher!=null){
				this.publisherMap.put(publisher.getId(), publisher);
			}
		}
		return publisher;
	}
	
	public String getPPubName(Integer pubId) {
		if(pubId==null){
			return null;
		}
		PPublisherBean publisher=this.getPPublisher(pubId);
		return publisher==null ? pubId.toString() : publisher.getSimpleName();
	}
	
}